﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using WarehouseWebAPP.code.BLL;

namespace WarehouseWebAPP.Pages.admin
{
    public partial class Dashboard : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {

        }

        protected void Timer1_Tick(object sender, EventArgs e)
        {
            NewsDataList.DataBind();
            ComponentGridView.DataBind();
            LendGridView.DataBind();
        }

        protected void ComponentGridView_DataBound(object sender, EventArgs e)
        {
            foreach (GridViewRow row in ComponentGridView.Rows)
            {
                Label vendor = (Label)row.FindControl("VendorLabel");
                Label supplier = (Label)row.FindControl("SupplierLabel");

                if ( supplier != null && vendor!=null)
                {
                    SupplierBLL supplierBLL = new SupplierBLL();
                    VendorBLL vendorBLL = new VendorBLL();
                    try
                    {

                        supplier.Text = supplierBLL.SupplierGetByNum(new Guid(supplier.Text))[0].supplier_name;
                        vendor.Text=vendorBLL.VendorGetByNum(new Guid(vendor.Text))[0].vendor_name;
                    }
                    finally
                    {
                        //TODO
                        //we should add dispose here
                        // we need to add dispose function foreach bll
                        // or maybe the detailsview disposes alone ,
                        // need to check, because there exists disposing event
                    }
                }
            }

        }

        protected void LendGridView_DataBound(object sender, EventArgs e)
        {
            foreach (GridViewRow row in LendGridView.Rows)
            {

                Label user = (Label)row.FindControl("UserLabel");
                Label customer = (Label)row.FindControl("CustomerLabel");
                Label item = (Label)row.FindControl("ItemLabel");

                if (user != null && customer != null && item!=null)
                {
                    UserBLL userBLL = new UserBLL();
                    CustomerBLL customerBLL = new CustomerBLL();
                    LendItemBLL itemBLL = new LendItemBLL();
                    try
                    {

                        user.Text = userBLL.userGetByNum(new Guid(user.Text))[0].user_name;
                        customer.Text = customerBLL.CustomerGetByNum(new Guid(customer.Text))[0].customer_name;
                        item.Text = itemBLL.LendItemByNum(new Guid(item.Text))[0].lend_item_serial;
                    }
                    finally
                    {
                        //TODO
                        //we should add dispose here
                        // we need to add dispose function foreach bll
                        // or maybe the detailsview disposes alone ,
                        // need to check, because there exists disposing event
                    }
                }
            }
        }

        protected void Timer2_Tick(object sender, EventArgs e)
        {
            ClockLabel.Text = "Date: "+DateTime.Now.ToShortDateString()+" Time: "+DateTime.Now.ToLongTimeString();
        }

        protected void ClockLabel_Load(object sender, EventArgs e)
        {
            ClockLabel.Text = "Date: " + DateTime.Now.ToShortDateString() + " Time: " + DateTime.Now.ToLongTimeString();
        }


 
    }
}
